//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/ActionDataImpl.java $
//$Id: ActionDataImpl.java 125 2013-04-07 11:42:35Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.DB;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.Location;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.mongo.data.MongoAction;
import de.piratech.dasding.data.database.mongo.data.MongoActionCategory;
import de.piratech.dasding.data.database.mongo.data.MongoEvent;
import de.piratech.dasding.data.database.mongo.data.MongoLocation;
import de.piratech.dasding.data.database.mongo.data.MongoUser;

/**
 */
public class ActionDataImpl extends AbstractMongoDB<MongoAction, Action>
    implements ActionData {

  protected ActionDataImpl(DB pDB) {
    super(pDB, MongoAction.class, COLLECTIONS.ACTION_COLLECTION);
  }

  @Override
  public Action createAction(Action pAction) throws DatabaseException {
    checkObjectExists(COLLECTIONS.EVENT_COLLECTION.getName(),
        pAction.getEvent());

    checkObjectExists(COLLECTIONS.ACTIONCATEGORY_COLLECTION.getName(),
        pAction.getCategory());

    checkObjectExists(COLLECTIONS.USER_COLLECTION.getName(),
        pAction.getContact());

    return getDBCollection().insert(new MongoAction(pAction)).getSavedObject()
        .getDataObject();
  }

  @Override
  public void deleteAction(Action pAction) throws DatabaseException {
    EventDataImpl eventDataImpl = new EventDataImpl(db);
    Event event = eventDataImpl.getEvent(pAction.getEvent().getId());
    event.removeAction(pAction);
    eventDataImpl.updateEvent(event);

    List<User> participants = pAction.getParticipants();
    for (User user : participants) {
      user.removeActionApplication(pAction.getId());
      new UserDataImpl(db).updateUser(user);
    }

    getDBCollection().removeById(pAction.getId());
  }

  @Override
  public void updateAction(Action pAction) {
    getDBCollection().updateById(pAction.getId(), new MongoAction(pAction));
  }

  @Override
  public Action getAction(String pId) throws DatabaseException {
    MongoAction findOneById = getDBCollection().findOneById(pId);
    if (findOneById != null) {
      return findOneById.getDataObject();
    } else {
      throw new DatabaseException("action does not exist");
    }
  }

  @Override
  public List<Action> getActions() {
    List<MongoAction> array = getDBCollection().find().toArray();
    return getDataObjectList(array);
  }

  @Override
  public List<Action> findActionsByCategories(List<String> pCategory)
      throws DatabaseException {
    List<Action> actions = new ArrayList<Action>();
    for (String id : pCategory) {
      List<MongoAction> array = findByObjectReference("categoryRef",
          COLLECTIONS.ACTIONCATEGORY_COLLECTION.getName(), id,
          MongoActionCategory.class);
      for (MongoAction mongoAction : array) {
        actions.add(mongoAction.getDataObject());
      }
    }
    return actions;

  }

  public List<Action> findActionByEvent(String eventId) {
    List<MongoAction> array = findByObjectReference("eventRef",
        COLLECTIONS.EVENT_COLLECTION.getName(), eventId, MongoEvent.class);
    return getDataObjectList(array);
  }

  public List<Action> findActionsByContact(User pUser) {
    List<MongoAction> array = findByObjectReference("contactRef",
        COLLECTIONS.USER_COLLECTION.getName(), pUser.getId(), MongoUser.class);
    return getDataObjectList(array);
  }

  public List<Action> findByLocation(Location pUser) {
    List<MongoAction> array = findByObjectReference("locationRef",
        COLLECTIONS.LOCATION_COLLECTION.getName(), pUser.getId(),
        MongoLocation.class);
    return getDataObjectList(array);
  }
}
